﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using BestCode.Creds.Business;
using BestCode.Creds.Entities;
using BestCode.Creds.Report.Dto;
using Microsoft.Reporting.WinForms;

namespace BestCode.Creds.UI.Report
{
    /// <summary>
    /// Interaction logic for VwReport.xaml
    /// </summary>
    public partial class VwReport : UserControl
    {
        public EntityChild CurrentEntityChild { get; set; }

        public VwReport(EntityChild entity)
        {
            InitializeComponent();
            CurrentEntityChild = entity;
            Loaded += VwReport_Loaded;
        }

        void VwReport_Loaded(object sender, RoutedEventArgs e)
        {
            ShowReport();
        }

        public byte[] ReadAllBytes(string fileName)
        {
            if (!File.Exists(fileName))
                return null;

            byte[] buffer = null;
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                buffer = new byte[fs.Length];
                fs.Read(buffer, 0, (int)fs.Length);
            }
            return buffer;
        }

        private void BtnShowReport_OnClick(object sender, RoutedEventArgs e)
        {
            ShowReport();
        }


        public void ShowReport()
        {
            ReportViewer viewer = new ReportViewer();
            viewer.SetDisplayMode(DisplayMode.PrintLayout);
            viewer.ProcessingMode = ProcessingMode.Local;

            if (rbtnSummary.IsChecked.Value)
            {
                viewer.LocalReport.ReportEmbeddedResource = "BestCode.Creds.UI.Report.Layout.Summary.rdlc";
                ReportReader reader = new ReportReader();

                var items = reader.GetSummary(CurrentEntityChild.Entity.Id);
                items.ForEach(p =>
                {
                    p.AnverseFace = ReadAllBytes(p.AnverseFacePath);
                    p.ReverseFace = ReadAllBytes(p.ReverseFacePath);
                }
                    );
                viewer.LocalReport.DataSources.Add(new ReportDataSource("DSSummary", items));
            }
            else if (rbtnDetailed.IsChecked.Value)
            {
                viewer.LocalReport.ReportEmbeddedResource = "BestCode.Creds.UI.Report.Layout.EnabledItems.rdlc";
                FieldDefinitionBusiness business = new FieldDefinitionBusiness();

                List<FieldDefinition> items = new List<FieldDefinition>();
                if (CurrentEntityChild.Id == -1)
                {
                    items = business.GetEnabledsByEntity(CurrentEntityChild.Entity.Id).ToList();
                }
                else
                {
                    var children = business.GetEnabledsByEntity(CurrentEntityChild.Entity.Id);
                    items = children.Where(p => p.EntityChild.Id == CurrentEntityChild.Id).ToList();
                }


                var dataSet = items.Select(p => new EnabledItemsDto()
                {
                    CI = p.CI,
                    ChildEntity = p.EntityChild != null ? p.EntityChild.Name : string.Empty,
                    FullName = p.FullName,
                    FullLastName = p.LastName1,
                    FullFirstName = p.FirstName
                    
                    /*Picture =
                        ReadAllBytes(p.EntityChild != null
                            ? p.EntityChild.PicturesFolder + "\\" + p.Picture
                            : p.Entity.PicturesFolder + "\\" + p.Picture)*/
                }).OrderBy(p=>p.FullLastName);
                viewer.LocalReport.DataSources.Add(new ReportDataSource("DSEnabledFields", dataSet));
            }
            else if (rbtnAllDatas.IsChecked.Value)
            {
                viewer.LocalReport.ReportEmbeddedResource = "BestCode.Creds.UI.Report.Layout.ListFieldItems.rdlc";
                FieldDefinitionBusiness business = new FieldDefinitionBusiness();

                List<FieldDefinition> items = new List<FieldDefinition>();
                
                items = business.GetByEntityId(CurrentEntityChild.Entity.Id).ToList();
                
                var dataSet = items.Select(p => new EnabledItemsDto()
                {
                    CI = p.CI,
                    ChildEntity = p.EntityChild != null ? p.EntityChild.Name : string.Empty,
                    FullName = p.FullName,
                    FullLastName = p.LastCompositeName,
                    FullFirstName = p.FirstCompositeName,
                    Code = p.Code,
                    Address = p.CellPhone
                    /*Picture =
                        ReadAllBytes(p.EntityChild != null
                            ? p.EntityChild.PicturesFolder + "\\" + p.Picture
                            : p.Entity.PicturesFolder + "\\" + p.Picture)*/
                }).OrderBy(p => p.FullLastName);
                viewer.LocalReport.DataSources.Add(new ReportDataSource("DSEnabledFields", dataSet));
            }



            this.hostReport.Child = viewer;
            viewer.RefreshReport();
        }


    }
}
